//
//  HamatonUpAndDownViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/12.
//  上下挂设置

import UIKit
import SnapKit

class HamatonUpAndDownViewController: HamatonBaseViewController {
    weak var delegate: HamatonUpAndDownDelegate?
    var trailerNum: String = "0"
    var trailersNumOption: trailerNumModel = trailerNumModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        navigationItem.title = "上下挂设置"
        
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 10
        
        view.addSubview(trailer1Label)
        view.addSubview(trailer1TextField)
        view.addSubview(lineView1)
        view.addSubview(trailer2Label)
        view.addSubview(trailer2TextField)
        view.addSubview(lineView2)
        view.addSubview(trailer3Label)
        view.addSubview(trailer3TextField)
        view.addSubview(lineView3)
        view.addSubview(trailer4Label)
        view.addSubview(trailer4TextField)
        view.addSubview(lineView4)
        view.addSubview(trailer5Label)
        view.addSubview(trailer5TextField)
        view.addSubview(lineView5)
        view.addSubview(trailer6Label)
        view.addSubview(trailer6TextField)
        view.addSubview(lineView6)
        view.addSubview(saveBtn)
        
        // 一号挂车
        trailer1Label.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer1TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer1Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(trailer1Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 二号挂车
        trailer2Label.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer2TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer2Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(trailer2Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 三号挂车
        trailer3Label.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer3TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer3Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(trailer3Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 四号挂车
        trailer4Label.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer4TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer4Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView4.snp.makeConstraints { make in
            make.top.equalTo(trailer4Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 五号挂车
        trailer5Label.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer5TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer5Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(trailer5Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 六号挂车
        trailer6Label.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        trailer6TextField.snp.makeConstraints { make in
            make.centerY.equalTo(trailer6Label.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        lineView6.snp.makeConstraints { make in
            make.top.equalTo(trailer6Label.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
//        // 保存设置
//        saveBtn.snp.makeConstraints { make in
//            make.top.equalTo(lineView6.snp.bottom).offset(margTop*3)
//            make.left.equalToSuperview().offset(26)
//            make.right.equalToSuperview().offset(-26)
//            make.height.equalTo(40)
//        }
        
        if self.trailerNum == "5" {
            trailer6Label.isHidden = true
            trailer6TextField.isHidden = true
            lineView6.isHidden = true
            
            self.setButtonPosition(lineView5)
            
        } else if self.trailerNum == "4" {
            trailer6Label.isHidden = true
            trailer6TextField.isHidden = true
            lineView6.isHidden = true
            
            trailer5Label.isHidden = true
            trailer5TextField.isHidden = true
            lineView5.isHidden = true
            
            self.setButtonPosition(lineView4)
        } else if self.trailerNum == "3" {
            trailer6Label.isHidden = true
            trailer6TextField.isHidden = true
            lineView6.isHidden = true
            
            trailer5Label.isHidden = true
            trailer5TextField.isHidden = true
            lineView5.isHidden = true
            
            trailer4Label.isHidden = true
            trailer4TextField.isHidden = true
            lineView4.isHidden = true
            
            self.setButtonPosition(lineView3)
        } else if self.trailerNum == "2" {
            trailer6Label.isHidden = true
            trailer6TextField.isHidden = true
            lineView6.isHidden = true
            
            trailer5Label.isHidden = true
            trailer5TextField.isHidden = true
            lineView5.isHidden = true
            
            trailer4Label.isHidden = true
            trailer4TextField.isHidden = true
            lineView4.isHidden = true
            
            trailer3Label.isHidden = true
            trailer3TextField.isHidden = true
            lineView3.isHidden = true
            
            self.setButtonPosition(lineView2)
        } else if self.trailerNum == "1" {
            trailer6Label.isHidden = true
            trailer6TextField.isHidden = true
            lineView6.isHidden = true
            
            trailer5Label.isHidden = true
            trailer5TextField.isHidden = true
            lineView5.isHidden = true
            
            trailer4Label.isHidden = true
            trailer4TextField.isHidden = true
            lineView4.isHidden = true
            
            trailer3Label.isHidden = true
            trailer3TextField.isHidden = true
            lineView3.isHidden = true
            
            trailer2Label.isHidden = true
            trailer2TextField.isHidden = true
            lineView2.isHidden = true
            
            self.setButtonPosition(lineView1)
        } else {
            self.setButtonPosition(lineView6)
        }
    }
    // 一号挂车
    private lazy var trailer1Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "一号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer1TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress1
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 二号挂车
    private lazy var trailer2Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "二号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer2TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress2
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 三号挂车
    private lazy var trailer3Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "三号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer3TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress3
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 四号挂车
    private lazy var trailer4Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "四号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer4TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress4
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 五号挂车
    private lazy var trailer5Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "五号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer5TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress5
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 六号挂车
    private lazy var trailer6Label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "六号挂车"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var trailer6TextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(14)
        textField.text = trailersNumOption.macAddress6
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    private lazy var lineView6: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 保存设置
    lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        return button
    }()
    
    // 保存按钮位置
    @objc func setButtonPosition(_ view: UIView) {
        // 保存设置
        saveBtn.snp.makeConstraints { make in
            make.top.equalTo(view.snp.bottom).offset(45)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
        }
    }
    func hamatonCarAddViewTextFieldChange(text: String?, type: Int) {
        if type == 1 {
            trailersNumOption.macAddress1 = text ?? ""
        } else if type == 2 {
            trailersNumOption.macAddress2 = text ?? ""
        } else if type == 3 {
            trailersNumOption.macAddress3 = text ?? ""
        } else if type == 4 {
            trailersNumOption.macAddress4 = text ?? ""
        } else if type == 5 {
            trailersNumOption.macAddress5 = text ?? ""
        } else if type == 6 {
            trailersNumOption.macAddress6 = text ?? ""
        }
    }

    // 输入框内容改变
    @objc func macAddressTextFieldDidChange(tf: UITextField) {
        if tf == trailer1TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tftext, type: 1)
        }else if tf == trailer2TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tf.text, type: 2)
        } else if tf == trailer3TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tf.text, type: 3)
        } else if tf == trailer4TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tf.text, type: 4)
        } else if tf == trailer5TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tf.text, type: 5)
        } else if tf == trailer6TextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.replacingOccurrences(of: ":", with: "").groupCharactersByTwoWithColon().prefix(17))
            tf.text = tftext
            hamatonCarAddViewTextFieldChange(text: tf.text, type: 6)
        }
    }
    
    // 保存
    @objc func onClickSaveButton() {
        print("保存上下挂设置")
        delegate?.didUpAndDownSave(trailersNumOption)
        self.view.makeToastActivity(.center)
        
    }
    // 挂车mac设置成功
    func macSettingSuccess() {
        self.view.makeToast("挂车mac设置成功", duration: 3.0, position: .center)
    }
    
}
